﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.Managers
{
    class PanelManager
    {
        Stack<Panels.BasePanel> pStack = new Stack<Panels.BasePanel>();
        public Panels.BasePanel currentPanel { get; private set; }

        public Panels.BasePanel GetStackTop()
        {
            return pStack.Peek();
        }

        public void AddPanel(Panels.BasePanel panel)
        {
            if (panel == null)
                return;
            if (currentPanel != null && currentPanel.isTemp)
                currentPanel.OnSleep();
            currentPanel = panel;
            if (!panel.isTemp)
            {
                if (pStack.Count > 0)
                    pStack.Peek().OnSleep();
                pStack.Push(panel);
            }
            else
            {
                if (pStack.Count > 0)
                    pStack.Peek().OnBackground();
            }
            currentPanel.OnAwake();
            Managers.Manager.clientForm.dataGridView1.Focus();
        }

        public void RemovePanel()
        {
            if (currentPanel.isTemp && pStack.Count>0)
            {
                currentPanel.OnSleep();
                currentPanel = pStack.Peek();
                currentPanel.OnForeground();
                return;
            }
            else if (pStack.Count > 1)
            {
                pStack.Pop().OnSleep();
                currentPanel = pStack.Peek();
                currentPanel.OnAwake();
            }
        }

        public void ClearPanels()
        {
            if (pStack.Count!=0 && currentPanel != null && currentPanel != pStack.Peek())
                currentPanel.OnSleep();
            currentPanel = null;

            for(int i = 0; i < pStack.Count; i++)
            {
                pStack.Pop().OnSleep();
            }
        }
    }
}
